<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/blog/lib/font-awesome/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    hostname: new URL('https://guodh.gitee.io/blog').hostname,
    root: '/blog/',
    scheme: 'Pisces',
    version: '7.7.1',
    exturl: false,
    sidebar: {"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},
    copycode: {"enable":true,"show_result":true,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    comments: {"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}
  };
</script>

  <meta name="description" content="Autowire 注解的使用最初发现的问题是，为什么 Autowire 会有告警，以为是无法注入，结果发现是可以注入的。那么也就是说 dubbo 的 bean 管理和 Spring 本质是同一个。 之后发现了个链接，对于dubbo如何管理bean产生了兴趣。 问题一：使用Spring的@Service 和 Dubbo的@Service暴露的是同一个Service吗？">
<meta property="og:type" content="article">
<meta property="og:title" content="BeanPostProcessor的神奇用法">
<meta property="og:url" content="https://guodh.gitee.io/blog/2020/04/25/2020/BeanPostProcessor%E7%9A%84%E7%A5%9E%E5%A5%87%E7%94%A8%E6%B3%95/index.html">
<meta property="og:site_name" content="蛋蛋的小屋">
<meta property="og:description" content="Autowire 注解的使用最初发现的问题是，为什么 Autowire 会有告警，以为是无法注入，结果发现是可以注入的。那么也就是说 dubbo 的 bean 管理和 Spring 本质是同一个。 之后发现了个链接，对于dubbo如何管理bean产生了兴趣。 问题一：使用Spring的@Service 和 Dubbo的@Service暴露的是同一个Service吗？">
<meta property="og:locale">
<meta property="article:published_time" content="2020-04-25T15:51:13.000Z">
<meta property="article:modified_time" content="2020-05-03T06:43:20.000Z">
<meta property="article:author" content="RunningEgg">
<meta property="article:tag" content="技术">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://guodh.gitee.io/blog/2020/04/25/2020/BeanPostProcessor%E7%9A%84%E7%A5%9E%E5%A5%87%E7%94%A8%E6%B3%95/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true
  };
</script>

  <title>BeanPostProcessor的神奇用法 | 蛋蛋的小屋</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">蛋蛋的小屋</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">来了就看看吧，没有刀剑只有故事</p>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-fw fa-home"></i>Home</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-fw fa-user"></i>About</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>Archives</a>

  </li>
        <li class="menu-item menu-item-book">

    <a href="/blog/book/" rel="section"><i class="fa fa-fw fa-archive"></i>book</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="https://guodh.gitee.io/blog/2020/04/25/2020/BeanPostProcessor%E7%9A%84%E7%A5%9E%E5%A5%87%E7%94%A8%E6%B3%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="RunningEgg">
      <meta itemprop="description" content="写下我的点点滴滴，写下我对这个世界的看法，还有我内心锁崇拜的技术">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="蛋蛋的小屋">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          BeanPostProcessor的神奇用法
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>

              <time title="Created: 2020-04-25 23:51:13" itemprop="dateCreated datePublished" datetime="2020-04-25T23:51:13+08:00">2020-04-25</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">Edited on</span>
                <time title="Modified: 2020-05-03 14:43:20" itemprop="dateModified" datetime="2020-05-03T14:43:20+08:00">2020-05-03</time>
              </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="Autowire-注解的使用"><a href="#Autowire-注解的使用" class="headerlink" title="Autowire 注解的使用"></a>Autowire 注解的使用</h1><p>最初发现的问题是，为什么 Autowire 会有告警，以为是无法注入，结果发现是可以注入的。那么也就是说 dubbo 的 bean 管理和 Spring 本质是同一个。</p>
<p>之后发现了个链接，对于dubbo如何管理bean产生了兴趣。</p>
<h2 id="问题一：使用Spring的-Service-和-Dubbo的-Service暴露的是同一个Service吗？"><a href="#问题一：使用Spring的-Service-和-Dubbo的-Service暴露的是同一个Service吗？" class="headerlink" title="问题一：使用Spring的@Service 和 Dubbo的@Service暴露的是同一个Service吗？"></a>问题一：使用Spring的@Service 和 Dubbo的@Service暴露的是同一个Service吗？</h2><a id="more"></a>
<p>是的。Dubbo的本质也是依托于Spring的容器管理。只是在Spring的暴露过程中，dubbo又做了一些事情。但是，由于Spring在处理循环依赖等问题上，对于Bean会有重新初始化的情况，所以Dubbo的暴露不能简单的使用 BeanPostProcessor。</p>
<blockquote>
<p>例如以下问题:<br><a href="https://github.com/apache/dubbo/issues/2215">https://github.com/apache/dubbo/issues/2215</a></p>
</blockquote>
<p>官方的回复是，dubbo 暴露的应该是Spring实现之后的类，所以在 BeaPostProssor中暴露Dubbo服务，是不太恰当的。会出现丢失 Aop 切面的情况。</p>
<h2 id="问题二：自己定义的BeanPostProcessor会不会存在顺序的问题"><a href="#问题二：自己定义的BeanPostProcessor会不会存在顺序的问题" class="headerlink" title="问题二：自己定义的BeanPostProcessor会不会存在顺序的问题"></a>问题二：自己定义的BeanPostProcessor会不会存在顺序的问题</h2><p>也是会存在的，但是和Spring内置的PP不影响，因为在 PostProcessorRegistrationDelegate#registerBeanPostProcessors 中可以看到，先是注册了实现order的PP，其次是没有实现的PP，最后才是内置的BeanPostProcessors。</p>
<p>同样的，建议都是实现Spring所提供的Adapter。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">InstantiationAwareBeanPostProcessorAdapter</span><br></pre></td></tr></table></figure>
<h2 id="Proxy的使用"><a href="#Proxy的使用" class="headerlink" title="Proxy的使用"></a>Proxy的使用</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Proxy.isProxyClass(testA.getClass())</span><br></pre></td></tr></table></figure>
<p>如果是Java DynamicProxy，可以使用如上面法来判断。因为Java在实现Proxy的时候，Java已经将代理在Cache之中，当然你也可以使用查看类名是否是<code>$</code>开头（手动狗头）。如果是cglib的，就是 <code>$$</code> 开头。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">AopUtils.isProxyClass(bean);</span><br></pre></td></tr></table></figure>
<p>AopUtils是在Spring中常常使用的。最初还以只是Proxy.isProxyClass的增强，结果完全不是。他判断的是 <code>SpringProxy</code>。这就陷入盲区了。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">AopProxyUtils#completeProxiedInterfaces</span><br><span class="line"></span><br><span class="line"><span class="keyword">boolean</span> addSpringProxy = !advised.isInterfaceProxied(SpringProxy.class);</span><br></pre></td></tr></table></figure>
<p>Spring在实现代理的时候，还让Bean实现了一个 <code>SpringProxy.class</code></p>
<h2 id="比较著名的扩展点-beanFactoryPostProcessor-的使用示例"><a href="#比较著名的扩展点-beanFactoryPostProcessor-的使用示例" class="headerlink" title="比较著名的扩展点 beanFactoryPostProcessor 的使用示例"></a>比较著名的扩展点 beanFactoryPostProcessor 的使用示例</h2><blockquote>
<p>使用扩展点比较好的开源项目有：apollo 和 dubbo<br>通过项目和Spring的结合，让bean的属性更具有弹性。</p>
</blockquote>
<p>来看apollo的实现，它向registerBeanDefination注册了很多的BeanFactoryPostProsscer 和 beanPostProsscer。</p>
<h4 id="BeanFactoryPostProsscer-的主要作用就是"><a href="#BeanFactoryPostProsscer-的主要作用就是" class="headerlink" title="BeanFactoryPostProsscer 的主要作用就是:"></a>BeanFactoryPostProsscer 的主要作用就是:</h4><ol>
<li>将${}的占位符，维护到apollo的维护表中(主要用户在配置文件，如application.properties)</li>
<li>创建 LocalResposity，以及 RemoteResposity</li>
<li>加载本地properties到${…}的属性中来</li>
</ol>
<h4 id="beanPostProsscer-的主要作用就是"><a href="#beanPostProsscer-的主要作用就是" class="headerlink" title="beanPostProsscer 的主要作用就是:"></a>beanPostProsscer 的主要作用就是:</h4><ol>
<li>遍历@Value的属性，并更新</li>
</ol>
<h2 id="Spring中的Aspect是如何实现的"><a href="#Spring中的Aspect是如何实现的" class="headerlink" title="Spring中的Aspect是如何实现的"></a>Spring中的Aspect是如何实现的</h2><p>我理解Spring中@Aspect的实现方式，并不是通过ASM来真正字节码强化Bean。</p>
<p>AspectJAutoProxyBeanDefinitionParser 和 BeanDefinitionParser 的用法。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ImportBeanDefinitionRegistrar</span><br></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>对于Spring而言，玩转 BeanFactoryPostProcessor和BeanPostProcessor是非常重要的。</p>
<ul>
<li>BeanFactoryPostProcessor 我们可以修改Bean的 Beandefination 就是Bean的元函数，创建 Beandefination(但是这个时候绝对不要去初始化Bean)。</li>
<li>BeanPostProcessor 能提供更多强化Bean的能力的作用</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/%E6%8A%80%E6%9C%AF/" rel="tag"># 技术</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2020/04/19/2020/InnoDB%E7%9A%84MVCC/" rel="prev" title="InnoDB的MVCC">
      <i class="fa fa-chevron-left"></i> InnoDB的MVCC
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2020/05/10/2020/RocketMq%E5%88%B7%E7%9B%98%E7%AD%96%E7%95%A5/" rel="next" title="RocketMq刷盘策略">
      RocketMq刷盘策略 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Autowire-%E6%B3%A8%E8%A7%A3%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-number">1.</span> <span class="nav-text">Autowire 注解的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%97%AE%E9%A2%98%E4%B8%80%EF%BC%9A%E4%BD%BF%E7%94%A8Spring%E7%9A%84-Service-%E5%92%8C-Dubbo%E7%9A%84-Service%E6%9A%B4%E9%9C%B2%E7%9A%84%E6%98%AF%E5%90%8C%E4%B8%80%E4%B8%AAService%E5%90%97%EF%BC%9F"><span class="nav-number">1.1.</span> <span class="nav-text">问题一：使用Spring的@Service 和 Dubbo的@Service暴露的是同一个Service吗？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%97%AE%E9%A2%98%E4%BA%8C%EF%BC%9A%E8%87%AA%E5%B7%B1%E5%AE%9A%E4%B9%89%E7%9A%84BeanPostProcessor%E4%BC%9A%E4%B8%8D%E4%BC%9A%E5%AD%98%E5%9C%A8%E9%A1%BA%E5%BA%8F%E7%9A%84%E9%97%AE%E9%A2%98"><span class="nav-number">1.2.</span> <span class="nav-text">问题二：自己定义的BeanPostProcessor会不会存在顺序的问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Proxy%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-number">1.3.</span> <span class="nav-text">Proxy的使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%AF%94%E8%BE%83%E8%91%97%E5%90%8D%E7%9A%84%E6%89%A9%E5%B1%95%E7%82%B9-beanFactoryPostProcessor-%E7%9A%84%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B"><span class="nav-number">1.4.</span> <span class="nav-text">比较著名的扩展点 beanFactoryPostProcessor 的使用示例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#BeanFactoryPostProsscer-%E7%9A%84%E4%B8%BB%E8%A6%81%E4%BD%9C%E7%94%A8%E5%B0%B1%E6%98%AF"><span class="nav-number">1.4.0.1.</span> <span class="nav-text">BeanFactoryPostProsscer 的主要作用就是:</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#beanPostProsscer-%E7%9A%84%E4%B8%BB%E8%A6%81%E4%BD%9C%E7%94%A8%E5%B0%B1%E6%98%AF"><span class="nav-number">1.4.0.2.</span> <span class="nav-text">beanPostProsscer 的主要作用就是:</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Spring%E4%B8%AD%E7%9A%84Aspect%E6%98%AF%E5%A6%82%E4%BD%95%E5%AE%9E%E7%8E%B0%E7%9A%84"><span class="nav-number">1.5.</span> <span class="nav-text">Spring中的Aspect是如何实现的</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-number">1.6.</span> <span class="nav-text">总结</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">RunningEgg</p>
  <div class="site-description" itemprop="description">写下我的点点滴滴，写下我对这个世界的看法，还有我内心锁崇拜的技术</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">89</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">24</span>
        <span class="site-state-item-name">tags</span>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="mailto:guodh_1@163.com" title="E-Mail → mailto:guodh_1@163.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">RunningEgg</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> v5.3.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">Theme – <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.7.1
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  















  

  

</body>
</html>
